import requests
import aiohttp
import asyncio
import time

def time_cost(tar,*args,**kmargs):
    print(tar.__name__,"开始时间",time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
    def inner(*args,**kmargs):
        tar(*args,**kmargs)
        print(tar.__name__,"结束时间",time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
        return
    return inner

@time_cost
def download_img(url):
    ret = requests.get(url)
    file_name = url.rsplit("/")[-1]
    with open('download/%s'%file_name,'wb') as cur_file:
        cur_file.write(ret.content)

async def main():
    url_list = [
        "https://img1.baidu.com/it/u=2679969194,2275948697&fm=253&fmt=auto&app=138&f=JPEG?w=600&h=450",
        "https://img2.baidu.com/it/u=2273231764,81652724&fm=253&fmt=auto&app=138&f=JPEG?w=1362&h=500",
    ]
    async with aiohttp.ClientSession() as session:
        tasks = [asyncio.create_task(async_download(session,url)) for url in url_list]
        await asyncio.wait(tasks)

async def async_download(session,url):
    async with session.get(url,verify_ssl=False) as response:
        content = await response.content.read()
        file_name = url.rsplit("=")[-1]
        with open('download/async%s.jpg'%file_name,'wb') as cur_file:
            cur_file.write(content)

if __name__ == "__main__":
#   url_list = [
#         "https://m.qkshu8.net/wen/1986615734/207.html",
#         "https://m.qkshu8.net/wen/1986615734/189.html",
#     ]
#   for url in url_list:
#     download_img(url)
  asyncio.get_event_loop().run_until_complete(main())